﻿@model Module
@{
    ViewBag.Title = (Model == null) ? "Create New Module" : "Edit Module";
    string startDate = "", endDate = "", hiddenSpeakers = "";
    if (Model != null)
    {
        //we need to set all the bindable properties so that the drop down items display their selected values
        Model.LevelId = Model.Level.Id;
        Model.VenueId = Model.Venue.Id;
        Model.SpeakerUserNames = Model.Speakers.Select(u => u.UserName).ToList();
        startDate = Model.Start.ToString(Module.DateFormat);
        endDate = Model.End.ToString(Module.DateFormat);
        hiddenSpeakers = String.Join(",", Model.Speakers.Select(s => s.UserName).ToArray());
    }
    
    EntitiesContainer container = ContextManager.CurrentDb;
    var levelSelect = container.Levels.ToList().Select(l => new SelectListItem { Text = l.Description, Value = l.Id.ToString() } );
    var venueSelect = container.Venues.OrderBy(v => v.Name).ToList().Select(v => new SelectListItem { Text = v.Name, Value = v.Id.ToString() });
    
    var speakerSelect = RoleHelper.Trainers()
        .OrderBy(u => u.LastName)
        .ThenBy(u => u.FirstName)
        .Select(u => new SelectListItem { Text = String.Format("{0}, {1}", u.LastName, u.FirstName), Value = u.UserName });
        
    
}

<p>Before creating a module, ensure the <a href="@Url.Action("Index", "Venue")">venue</a> has been created, and that all speakers at the module have existing <a href="@Url.Action("UserList", "Admin")">accounts</a>.</p>

@using (Html.BeginForm("Edit", "Module"))
{
    @Html.ValidationSummary(true, "Please correct the errors below and try again.")
    <div>
        <fieldset>
            <legend>Module Details</legend>
            <div class="editor-label">
                @Html.LabelForWithReq(m => m.Title)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(m => m.Title)
                @Html.ValidationMessageFor(m => m.Title)
            </div>

            <div class="editor-label">
                @Html.LabelForWithReq(m => m.Teaser)
            </div>
            <div class="editor-field">
                @Html.TextAreaFor(m => m.Teaser, new { @class = "adminBasicText" })
            </div>
            <div class="editor-label">
                @Html.LabelForWithReq(m => m.Description)
            </div>
            <div class="editor-field">
                @Html.TextAreaFor(m => m.Description, new { @class = "adminRichText" })
            </div>

            <div class="editor-label">
                @Html.LabelForWithReq(m => m.Price)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(m => m.Price, new { @class = "price" })
                @Html.ValidationMessageFor(m => m.Price)
            </div>

            <div class="editor-label">
                @Html.LabelForWithReq(m => m.LevelId)
            </div>
            <div class="editor-field">
                @Html.DropDownListFor(m => m.LevelId, levelSelect.AddPleaseSelectItem())
                @Html.ValidationMessageFor(m => m.LevelId)
            </div>

            <div class="editor-label">
                @Html.LabelForWithReq(m => m.Start)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(m => m.Start, new { @class = "date-time", @Value = startDate })
                @Html.ValidationMessageFor(m => m.Start)
            </div>
            <div class="editor-label">
                @Html.LabelForWithReq(m => m.End)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(m => m.End, new { @class = "date-time", @Value = endDate })
                @Html.ValidationMessageFor(m => m.End)
            </div>
            <div class="editor-label">
                @Html.LabelForWithReq(m => m.VenueId)
            </div>
            <div class="editor-field">
                @Html.DropDownListFor(m => m.VenueId, venueSelect.AddPleaseSelectItem())
                @Html.ValidationMessageFor(m => m.VenueId)
            </div>
            <div class="editor-label">
                @Html.LabelForWithReq(m => m.ContactEmail)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(m => m.ContactEmail)
                @Html.ValidationMessageFor(m => m.ContactEmail)
            </div>
            <div class="editor-label">
                @Html.LabelForWithReq(m => m.SpeakerUserNames)
            </div>
            <div class="editor-field">
                @Html.DropDownListFor(m => m.SpeakerUserNames, speakerSelect, new { multiple = "multiple", @class = "selectSpeakers" })
                @Html.ValidationMessageFor(m => m.SpeakerUserNames)
                @if (!String.IsNullOrWhiteSpace(hiddenSpeakers))
                {
                    <script type="text/javascript">
                        (function() {
                            var toSelect = '@(hiddenSpeakers)'.split(',');
                            for (var i = 0; i < toSelect.length; i++)
                            {
                                $('.selectSpeakers option[value="' + toSelect[i] +'"]').attr('selected', 'selected');
                            }
                        })();
                    </script>
                }
            </div>
            <div class="editor-label">
                @Html.LabelForWithReq(m => m.MaxEnrollment)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(m => m.MaxEnrollment)
                @Html.ValidationMessageFor(m => m.MaxEnrollment)
            </div>
            <div class="editor-label check">
                @{ object htmlAttributes = (Model == null) ? new { @checked = "checked" } : null; }
                @Html.LabelFor(m => m.Active) @Html.CheckBoxFor(m => m.Active, htmlAttributes)
            </div>

            <div class="editor-label check">
                @{ object featuredAttr = (Model != null && Model.Featured) ? new { @checked = "checked" } : null; }
                @Html.LabelFor(m => m.Featured) @Html.CheckBoxFor(m => m.Featured, featuredAttr) <span class="note">(Displays module on home page)</span>
            </div>

            @Html.Partial("RelatedProductSelect", Model)

            @if (Model != null) { <text>@Html.HiddenFor(m => m.Id)</text> }
            <p class="buttons">
                <input type="submit" value="Submit" />
            </p>
        </fieldset>
    </div>
}